import * as API from '../../api'
import * as types from '../mutation-types'
const state = {
    state: {
        userList: {
            list: [],
            page: 0,
            total: 0
        },
        userData: {},
        vipList: [],
        inviteList: {
            list: [],
            page: 0,
            total: 0
        },
        updateUserDetail: null,
        buyVIP: null
    },
    mutations: {
        [types.GET_USER_LIST_SUCCESS] (state, payload) {
            state.userList = payload
        },
        [types.GET_USER_DATA_SUCCESS] (state, payload) {
            state.userData = payload
        },
        [types.GET_USER_VIP_RECORD_DATA_SUCCESS] (state, payload) {
            state.vipList = payload
        },
        [types.GET_USER_INVITE_RECORD_DATA_SUCCESS] (state, payload) {
            state.inviteList = payload
        },
        [types.UPDATE_USER_DETAIL] (state,payload) {
          state.updateUserDetail = payload
        },
        [types.USER_BUY_VIP] (state,payload){
            state.buyVIP = payload
        }
    },
    actions: {
        getUserList({commit,state,rootState}, payload) {
            var prom = API.getUserList(payload)
            prom.then(function (data) {
                commit(types.GET_USER_LIST_SUCCESS, data.result)
                rootState.reqError = null
            })
            .catch(function (err) {
            rootState.reqError = err
            })
            .finally(function () {
            rootState.reqShake++
            })
            return prom
        },
        updateUserDetail({commit,state,rootState}, params) {
          var prom = API.updateUserDetail(params)
          prom.then(function (data) {
            commit(types.UPDATE_USER_DETAIL, data.result)
            rootState.reqError = null
          })
            .catch(function (err) {
              rootState.reqError = err
            })
            .finally(function () {
              rootState.reqShake++
            })
          return prom
        },
        userBuyVIP({commit,state,rootState}, params) {
          var prom = API.userBuyVip(params)
          prom.then(function (data) {
            commit(types.USER_BUY_VIP, data.result)
            rootState.reqError = null
          })
            .catch(function (err) {
              rootState.reqError = err
            })
            .finally(function () {
              rootState.reqShake++
            })
          return prom
        },
        getUserDetail({commit,state,rootState}, userid) {
            var prom = API.getUserDetail(userid)
            prom.then(function (data) {
                commit(types.GET_USER_DATA_SUCCESS, data.result)
                rootState.reqError = null
            })
            .catch(function (err) {
            rootState.reqError = err
            })
            .finally(function () {
            rootState.reqShake++
            })
            return prom
        },
        getVipRecordList({commit,state,rootState}, userid) {
            var prom = API.getVipRecordList(userid)
            prom.then(function (data) {
                commit(types.GET_USER_VIP_RECORD_DATA_SUCCESS, data.result)
                rootState.reqError = null
            })
            .catch(function (err) {
            rootState.reqError = err
            })
            .finally(function () {
            rootState.reqShake++
            })
            return prom
        },
        getInviteRecordList({commit,state,rootState}, userid) {
            var prom = API.getInviteRecordList(userid)
            prom.then(function (data) {
                commit(types.GET_USER_INVITE_RECORD_DATA_SUCCESS, data.result)
                rootState.reqError = null
            })
            .catch(function (err) {
            rootState.reqError = err
            })
            .finally(function () {
            rootState.reqShake++
            })
            return prom
        }
    },
    getters: {

    }
}
export default state
